package com.cheer.ba.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.model.BankModel;
import com.cheer.ba.presenter.impl.AskQuestPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hq on 2017/7/31.
 * 申请提现
 */
public class AskGetMoneyAty extends BaseCommAty {

    //提现金额
    @BindView(R.id.id_get_money_txt)
    TextView mTxtGetMoney;

    @BindView(R.id.id_input_money_edit)
    EditText mEditMoney;
    //记录要提现的金额
    private String mSaveMoney;

    //有银行卡
    @BindView(R.id.id_my_default_card_rela)
    RelativeLayout mRelaHadBank;
    @BindView(R.id.id_bank_logo_img)
    ImageView mImgLogo;
    @BindView(R.id.id_bank_name_txt)
    TextView mTxtBankName;
    @BindView(R.id.id_bank_card_num_txt)
    TextView mTxtBankNum;
    //存储银行卡号
    private String mSaveBankNum;

    //没有银行卡
    @BindView(R.id.id_no_bank_linear)
    LinearLayout mLinearNoBank;

    //可提现的金额
    private String mGetMoney;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_ask_getmoney, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initPresenter() {
        presenter = new AskQuestPresenter();
    }

    @Override
    public void initView() {
        setTitleText("申请提现");
        setTitleRight("提现记录", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(aty, LookMyBillAty.class);
                intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_MONEY);
                showActivity(aty, intent);
            }
        });
        mGetMoney = getIntent().getStringExtra(IntentKey.General.KEY_PAY_MONEY);
        if (!StringUtils.isEmpty(mGetMoney)) {
            mTxtGetMoney.setText(Currency.returnDollar(mGetMoney));
        } else {
            mTxtGetMoney.setText("0元");
        }
        mEditMoney.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(".") && dest.toString().length() == 0) {
                    return "0.";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        ((AskQuestPresenter) presenter).getBankList(false);
    }

    @OnClick({R.id.id_get_all_money_txt, R.id.id_get_bank_rela, R.id.id_my_default_card_rela, R.id.id_no_bank_linear, R.id.id_get_money_btn})
    public void onClick(View mView) {
        switch (mView.getId()) {
            case R.id.id_get_all_money_txt:
                //全部提现
                mEditMoney.setText(Currency.returnDollar(mGetMoney).replace("元", ""));
                break;
            case R.id.id_get_bank_rela:
            case R.id.id_my_default_card_rela:
            case R.id.id_no_bank_linear:
                //进入银行卡界面
                showActivity(aty, MyBankListAty.class);
                break;
            case R.id.id_get_money_btn:
                //确认提现
                if (StringUtils.isEmpty(mGetMoney) || Double.parseDouble(mGetMoney) <= 0) {
                    SVProgressHUD.showInfoWithStatus(aty, "暂无可提现金额");
                } else {
                    mSaveMoney = mEditMoney.getText().toString().trim();
                    if (StringUtils.isEmpty(mSaveMoney)) {
                        SVProgressHUD.showInfoWithStatus(aty, "请输入提现金额");
                    } else {
                        final int money = (int) (Double.parseDouble(mSaveMoney) * 100);
                        int total_money = (int) Double.parseDouble(mGetMoney);
                        if (money <= 0) {
                            SVProgressHUD.showInfoWithStatus(aty, "请输入提现金额");
                        } else {
                            if (money > total_money) {
                                SVProgressHUD.showInfoWithStatus(aty, "超出可提现金额");
                            } else {
                                if (StringUtils.isEmpty(mSaveBankNum)) {
                                    SVProgressHUD.showInfoWithStatus(aty, "请选择银行卡");
                                } else {
                                    //提现
                                    ((AskQuestPresenter) presenter).getMoney(true, mSaveBankNum, money + "");
                                }
                            }
                        }
                    }
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        //返回类型
        String type = event.type;
        if (TextUtils.equals(type, ConstantTag.GET_MY_BANKS)) {
            //我的银行卡
            final BankModel model = (BankModel) event.model;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (null == model) {
                        mRelaHadBank.setVisibility(View.GONE);
                        mLinearNoBank.setVisibility(View.VISIBLE);
                    } else {
                        mRelaHadBank.setVisibility(View.VISIBLE);
                        mLinearNoBank.setVisibility(View.GONE);
                        mImgLogo.setVisibility(View.GONE);
                        mTxtBankName.setText("");
                        mSaveBankNum = model.bankNo;
                        mTxtBankNum.setText(StringUtils.replaceBankString(mSaveBankNum, mSaveBankNum.length()-4));
                    }
                }
            });
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_MY_BANKS)) {
                //获得我的银行卡
                final List<BankModel> banks = (List<BankModel>) t;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (banks.isEmpty()) {
                            mRelaHadBank.setVisibility(View.GONE);
                            mLinearNoBank.setVisibility(View.VISIBLE);
                        } else {
                            mRelaHadBank.setVisibility(View.VISIBLE);
                            mLinearNoBank.setVisibility(View.GONE);
                            mImgLogo.setVisibility(View.GONE);
                            BankModel model = banks.get(0);
                            mTxtBankName.setText("");
                            mSaveBankNum = model.bankNo;
                            mTxtBankNum.setText(StringUtils.replaceBankString(mSaveBankNum, mSaveBankNum.length()-4));
                        }
                    }
                });
            } else if (TextUtils.equals(tag, ConstantTag.GET_MONEY_SUCCESS)) {
                //提现成功
                SVProgressHUD.showSuccessWithStatus(aty, "提现申请成功,将会在一个工作日内到账");
                BaseEvent event = new BaseEvent();
                event.type = ConstantTag.GET_MONEY_SUCCESS;
                EventBus.getDefault().post(event);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 2000);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
